package com.mimiwang.comment.service.Imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mimiwang.comment.dao.AnimalCommentDao;
import com.mimiwang.comment.dao.CommentDao;
import com.mimiwang.comment.entity.AniComment;
import com.mimiwang.comment.entity.Comment;
import com.mimiwang.comment.service.AnimalCommentService;
import com.mimiwang.comment.vo.CommentVo;
import com.mimiwang.common.vo.school.PageConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @AUTHOR:游李滨
 * @DATE:2023/5/2 19:34
 */

@Service
public class AnimalCommentServiceImpl implements AnimalCommentService {

    @Autowired
    AnimalCommentDao animalCommentDao;

    @Autowired
    CommentDao commentDao;
    private void sort(List<CommentVo> lst){
        lst.sort((a,b)->{

//            首先比较点赞数
            if( a.getComment().getLikeNum() > b.getComment().getLikeNum()){
                return -1;
            }else if(a.getComment().getLikeNum() < b.getComment().getLikeNum()){
                return 1;
            }else{
//                其次比较创建时间
                long l = a.getComment().getCreateTime().getTime() - b.getComment().getCreateTime().getTime();
                return (int) l;
            }
        });
    }
    /**
     * 根据动物id查询所有一级评论
     *
     * */
    @Override
    public PageInfo<CommentVo> selectCommentListByAnimalId(long animalId, PageConfig pageConfig) {
        PageHelper.startPage(pageConfig);
//        查找所有一级评论,每条评论只有id属性
        List<AniComment> comments = animalCommentDao.selectCommentByAnimalId(animalId);



        List<CommentVo> lst = new ArrayList<>();
        for (AniComment aniComment : comments) {
//            查找一级评论的vo

            CommentVo commentVo = commentDao.selectCommentVoById(aniComment.getCmId());
            lst.add(commentVo);
        }
        this.sort(lst);
        return new PageInfo<>(lst);
    }
    /**
     * 根据父评论id查询所有子评论
     *
     * */
    @Override
    public PageInfo<CommentVo> selectChildCommentListByParentId(long parentId, PageConfig pageConfig) {
        PageHelper.startPage(pageConfig);
        List<CommentVo> commentVos = commentDao.selectChildCommentListByParentId(parentId);
        return new PageInfo<>(commentVos);
    }


    private void initComment(Comment comment){
        //        设置创建时间
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        comment.setCreateTime(timestamp);

//        TODO 设置发送者


    }


    @Transactional
    @Override
    public boolean insertAnimalComment(Comment comment, int animalId) {
//        初始化一些参数
        this.initComment(comment);

//        设置层次
        comment.setLevel(1);

//        设置接收者id为1,无意义，主要是防止联表查不到user信息会导致该条评论无法显示
        comment.setReceiverId(1);
//        创建评论
        int cm = commentDao.insertComment(comment);

//        创建 动物-评论的关联
        AniComment aniComment = new AniComment();
        aniComment.setAniId(animalId);
        aniComment.setCmId(comment.getId());
        int aniCm = animalCommentDao.insert(aniComment);
        return (cm+aniCm)==2;
    }
    /**
     *  父评论id，发送者id，发送内容content，发送层次level
     *
     * */

    @Override
    public boolean insertReplyComment(Comment comment) {
        this.initComment(comment);
        comment.setLevel(comment.getLevel()+1);
        int i = commentDao.insertComment(comment);
        return i==1;
    }

    @Override
    public boolean deleteComment(int commentId) {
        return false;
    }
}
